//
// Created by mi on 2024/2/3.

#include <iostream>
#include <asio.hpp>

using namespace asio;
using namespace std;

int main() {
    io_context context;
    ip::tcp::resolver resolver = ip::tcp::resolver(context);
    ip::tcp::tcp::socket socket = ip::tcp::socket(context);

    ip::tcp::resolver::results_type endpoints;
    std::string ip("127.0.0.1");
    std::string port("9999");
    char buf[1024];
    try {
        endpoints = resolver.resolve(ip::tcp::tcp::v4(), ip, port);
        connect(socket, endpoints);
    }
    catch (std::exception &e) {
        cout << e.what() << std::endl;
    }
    for (int i = 0; i < 5; i++) {
        socket.send(buffer("hello word"));
        memset(buf, 0, sizeof(buf));
        socket.receive(buffer(buf, sizeof(buf)));
        std::cout << buf << std::endl;
    }
    socket.close();
    context.stop();
    return 0;
}